import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;

import java.awt.*;
import java.io.*;

import javax.imageio.*;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;

import java.awt.image.BufferedImage;
/**
*   PlayerFrame class
*   @author Kamil Olejnik
*/
class BetFrame extends JDialog{
        private JTextField txtBet;

        BetFrame(PlayerFrame frame){

                super(frame,false);
                setBounds(320,250,240,140);
                JPanel panel = new JPanel(new GridLayout(2,1,5,5));
                txtBet = new JTextField();
                JButton btnConfirm = new JButton("Confirm");
                btnConfirm.addActionListener(frame);
                panel.add(txtBet);
                panel.add(btnConfirm);
                add(panel);
        }
        /**
        * @return txtBet
        */
        public String getBet(){
                return txtBet.getText();
        }
        public void clear(){
                this.txtBet.setText("");
        }
}


public class PlayerFrame extends JFrame implements ActionListener
{
        
        private JPanel contentPane;
        private JPanel[] panels;
        private JLabel[] lblPlayers;
        private JLabel[] lblPlayersCoins;
        private JLabel[] lblPlayersBets;
        private JLabel[] lblPlayersExchangeCards;
        private JLabel[] Players;
        private JLabel lblMyCoins;
        private JLabel lblMyBet;
        private JLabel lblRound;
        private JLabel lblStake;
        private JLabel lblMaxBet;
        private JButton btnCheck;
        private JButton[] rejectPlayer;
        private JButton[] addBot;
        private JButton startGame;
        private JButton btnBet;
        private JButton btnRaise;
        private JButton btnCall;
        private JButton btnFold;
        private JButton btnAllin;
        private JButton btnExchange;
        private BetFrame betFrame;
        private String[] playersNames;
        private HumanListener listener;
    private JScrollPane scrollPane;
     /**
   * The PrevCounter
   * 
   * @var  counterPrev Previous counter
   */
        private int counterPrev = 0;
        /**
   * The NextCounter
   * 
   * @var  counterNext Next counter
   */
        private int counterNext = 0;
        /**
   * The myBet
   * 
   * @var myBet my current bet
   */
        private int myBet = 0;
         /**
   * The maxBet
   * 
   * @var maxBet max bet
   */
        private int maxBet = 0;
        private int myCoins;
        private JTextArea txtaChatDisplay;
        private JTextField txtChat;
        private String myName;
    private ImageIcon[] cards;
    private BufferedImage[] cardsImages;
    private JToggleButton[] cardsButtons;
    private Border selectedCard = BorderFactory.createLineBorder(Color.blue, 2);
    private String cardSet;
    private JPanel mainPanel;
    private JPanel resultPanel;
    private JPanel[] playersPanels;
    private JLabel[][] playersCardsLabels;
    private JButton closingButton;
    private boolean start;


        /**
        * @param name PlayerName
        * @param listener Human Listener
         * Create the frame.
         */
        public PlayerFrame(String name, HumanListener listener)
        {       this.start=false;
                this.myName = name;
                lblPlayers = new JLabel[5];
                lblPlayersCoins = new JLabel[5];
                lblPlayersBets = new JLabel[5];
                lblPlayersExchangeCards = new JLabel[5];
                panels = new JPanel[6];
        cards = new ImageIcon[54];
        cardsImages = new BufferedImage[54];
        cardsButtons = new JToggleButton[4];
                playersNames = new String[]{"","","","",""};
                this.listener = listener;
                this.betFrame = new BetFrame(this);
        final PlayerFrame temp = this;
        this.setTitle("Badugi");
            this.setResizable(false);

            resultPanel = new JPanel (new BorderLayout());
        mainPanel = new JPanel(new GridLayout(3,2,5,5));
        playersPanels = new JPanel[6];
        playersCardsLabels = new JLabel[6][4];
        closingButton = new JButton("Close");
        closingButton.addActionListener(this);
        
        for(int i = 0; i < 6; i++) {
            playersPanels[i] = new JPanel();
            playersPanels[i].setSize(72,400);
            playersPanels[i].setBackground(new Color(5,75,8));
            for(int j = 0; j < 4; j++) {
                playersCardsLabels[i][j] = new JLabel();
                playersCardsLabels[i][j].setHorizontalAlignment(JLabel.CENTER);
                playersCardsLabels[i][j].setVerticalAlignment(JLabel.CENTER);
                playersCardsLabels[i][j].setIcon(getCardIcon(52));
                playersPanels[i].add(playersCardsLabels[i][j]);
            }


            playersPanels[i].setLayout(new FlowLayout());
            mainPanel.add(playersPanels[i]);
        }
        mainPanel.setBackground(new Color(5,75,8));
        resultPanel.add(mainPanel,BorderLayout.CENTER);
        resultPanel.add(closingButton,BorderLayout.SOUTH);
        this.add(resultPanel);

        for(int i = 0; i < 4; i++) {
            cardsButtons[i] = new JToggleButton();
            cardsButtons[i].setActionCommand(Integer.toString(i));
            cardsButtons[i].addActionListener(this);
        }
        try {
            for(int i = 0; i < cardsImages.length; i++) {
                cards[i] = new ImageIcon();
                File tmp = new File("./Images/" + Integer.toString(i+1)+".jpg"); //works on linux
                cardsImages[i] = ImageIO.read(tmp);//folder z obrazkami i numerowac je od 0-52
                cards[i].setImage(cardsImages[i]);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setBounds(100, 100, 900, 600);
                contentPane = new JPanel();
                contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                contentPane.setBackground(new Color(5,75,8));
                setContentPane(contentPane);
                contentPane.setLayout(null);
                
                JPanel pnlPlayersList = new JPanel(new GridLayout(5,5));
                pnlPlayersList.setBorder(new TitledBorder(null, "Players", TitledBorder.LEADING, TitledBorder.TOP, null, null));
                pnlPlayersList.setBounds(594, 336, 278, 180);
                contentPane.add(pnlPlayersList);
               // pnlPlayersList.setLayout(null);
                Players = new JLabel[5];
                rejectPlayer = new JButton[5];
                addBot = new JButton[5];
                for(int i = 0; i<5; i++)
                {
                	Players[i] = new JLabel("Empty");
                	pnlPlayersList.add(Players[i]);
                	rejectPlayer[i] = new JButton("Delete");
                	rejectPlayer[i].setEnabled(false);
                	rejectPlayer[i].addActionListener(this);
                	rejectPlayer[i].setActionCommand("Delete"+i);
                	pnlPlayersList.add(rejectPlayer[i]);
                	addBot[i] = new JButton("Add Bot");
                	addBot[i].setEnabled(false);
                	addBot[i].addActionListener(this);
                	pnlPlayersList.add(addBot[i]);
                }
                startGame = new JButton("Start Game");
            	startGame.addActionListener(this);
            	startGame.setBounds(594, 526, 278, 30);
            	startGame.setEnabled(false);
            	contentPane.add(startGame);
            	
                JPanel pnlChat = new JPanel();
                pnlChat.setBackground(new Color(9,47,5));
                pnlChat.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Chat", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(51, 204, 51)));
                pnlChat.setBounds(594, 12, 278, 314);
                contentPane.add(pnlChat);
                pnlChat.setLayout(null);
                
                txtaChatDisplay = new JTextArea(15,10);
                scrollPane = new JScrollPane(txtaChatDisplay);
                txtaChatDisplay.setLineWrap(true);
                scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                txtaChatDisplay.setBackground(new Color(192, 192, 192));
                txtaChatDisplay.setEditable(false);
                scrollPane.setBounds(10, 16, 258, 230);
                pnlChat.add(scrollPane);
                
                txtChat = new JTextField("");
                txtChat.setBounds(10, 245, 258, 31);
                pnlChat.add(txtChat);
                
                JButton btnSend = new JButton("Send");
                btnSend.addActionListener(this);
                btnSend.setBounds(10, 274, 258, 30);
                pnlChat.add(btnSend);
                
                JPanel pnlMe = new JPanel();
                pnlMe.setBorder(new TitledBorder(null, name, TitledBorder.LEADING, TitledBorder.TOP, null, null));
                pnlMe.setBounds(26, 339, 541, 206);
                contentPane.add(pnlMe);
                pnlMe.setLayout(null);




                cardsButtons[0].setBounds(8, 103, 72, 96);
        cardsButtons[0].setHorizontalAlignment(SwingConstants.CENTER);
        cardsButtons[0].setIcon(cards[52]);
                pnlMe.add(cardsButtons[0]);


        cardsButtons[1].setBounds(92, 103, 72, 96);
        cardsButtons[1].setHorizontalAlignment(SwingConstants.CENTER);
        cardsButtons[1].setIcon(cards[52]);
                pnlMe.add(cardsButtons[1]);


        cardsButtons[2].setBounds(174, 103, 72, 96);
        cardsButtons[2].setHorizontalAlignment(SwingConstants.CENTER);
        cardsButtons[2].setIcon(cards[52]);
                pnlMe.add(cardsButtons[2]);


        cardsButtons[3].setBounds(256, 103, 72, 96);
        cardsButtons[3].setHorizontalAlignment(SwingConstants.CENTER);
        cardsButtons[3].setIcon(cards[52]);
                pnlMe.add(cardsButtons[3]);
                
                JPanel pnlButtons = new JPanel();
                pnlButtons.setBounds(428, 16, 105, 183);
                pnlMe.add(pnlButtons);
                pnlButtons.setLayout(new GridLayout(0, 1, 0, 0));
                
                btnCheck = new JButton("Check");
                btnCheck.addActionListener(this);
                pnlButtons.add(btnCheck);
                
                btnBet = new JButton("Bet");
                btnBet.addActionListener(this);
                pnlButtons.add(btnBet);
        
                btnRaise = new JButton("Raise");
                btnRaise.addActionListener(this);
                pnlButtons.add(btnRaise);
                
                btnCall = new JButton("Call");
                btnCall.addActionListener(this);
                pnlButtons.add(btnCall);
                
                btnFold = new JButton("Fold");
                btnFold.addActionListener(this);
                pnlButtons.add(btnFold);
                
                btnAllin = new JButton("All-in");
                btnAllin.addActionListener(this);
                pnlButtons.add(btnAllin);
                
                btnExchange = new JButton("Exchange");
                btnExchange.addActionListener(this);
                pnlButtons.add(btnExchange);
                
                JLabel lblNewLabel = new JLabel("My Coins:");
                lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
                lblNewLabel.setBounds(8, 16, 87, 31);
                pnlMe.add(lblNewLabel);
                
                lblMyCoins = new JLabel("<AMOUNT>");
                lblMyCoins.setHorizontalAlignment(SwingConstants.CENTER);
                lblMyCoins.setBounds(95, 16, 105, 31);
                pnlMe.add(lblMyCoins);
                
                lblMyBet = new JLabel("0");
                lblMyBet.setHorizontalAlignment(SwingConstants.CENTER);
                lblMyBet.setBounds(313, 16, 105, 31);
                pnlMe.add(lblMyBet);
                
                JLabel lblNewLabel2 = new JLabel("My Bet:");
                lblNewLabel2.setHorizontalAlignment(SwingConstants.CENTER);
                lblNewLabel2.setBounds(225, 16, 87, 31);
                pnlMe.add(lblNewLabel2);
                
                JPanel pnlPlayers = new JPanel(new GridLayout(2, 3, 10, 10));
                pnlPlayers.setBounds(26, 22, 541, 292);
                pnlPlayers.setBackground(new Color(5,75,8));
                contentPane.add(pnlPlayers);
                
                for( int i=1; i<4; i++){
                        panels[i]= new JPanel(new GridLayout(4,1));
                        panels[i].setBackground(new Color(5,75,8));
            panels[i].setForeground(new Color(253, 253, 253));
                        lblPlayers[i] = new JLabel("");
                        lblPlayers[i].setHorizontalAlignment(SwingConstants.CENTER);
                        lblPlayersCoins[i] = new JLabel();
                        lblPlayersCoins[i].setHorizontalAlignment(SwingConstants.RIGHT);
                        lblPlayersBets[i] = new JLabel();
                        lblPlayersBets[i].setHorizontalAlignment(SwingConstants.RIGHT);
                        lblPlayersBets[i].setForeground(Color.WHITE);
                        lblPlayersExchangeCards[i] = new JLabel();
                        //lblPlayersExchangeCards[i].setFont(new Font("Tahoma", Font.PLAIN, 8));
                        panels[i].add(lblPlayers[i]);
                        panels[i].add(lblPlayersCoins[i]);
                        panels[i].add(lblPlayersBets[i]);
                        panels[i].add(lblPlayersExchangeCards[i]);
                        pnlPlayers.add(panels[i]);
                }
                
                panels[0]= new JPanel(new GridLayout(4,1));
                panels[0].setBackground(new Color(5,75,8));
        panels[0].setForeground(new Color(253, 253, 253));
                lblPlayers[0] = new JLabel("");
                lblPlayers[0].setHorizontalAlignment(SwingConstants.CENTER);
                lblPlayersCoins[0] = new JLabel();
                lblPlayersCoins[0].setHorizontalAlignment(SwingConstants.RIGHT);
                lblPlayersBets[0] = new JLabel();
                lblPlayersBets[0].setHorizontalAlignment(SwingConstants.RIGHT);
                //lblPlayersBets[0].setVerticalAlignment(SwingConstants.TOP);
                //lblPlayersBets[0].setFont(new Font("Tahoma", Font.BOLD, 10));
                lblPlayersBets[0].setForeground(Color.WHITE);
                lblPlayersExchangeCards[0] = new JLabel();
                panels[0].add(lblPlayers[0]);
                panels[0].add(lblPlayersCoins[0]);
                panels[0].add(lblPlayersBets[0]);
                panels[0].add(lblPlayersExchangeCards[0]);
                pnlPlayers.add(panels[0]);
                
                panels[5] = new JPanel(new GridLayout(3, 1, 5, 5));
                panels[5].setBackground(new Color(5,75,8));
                
                lblRound = new JLabel("Round: 1");
                lblRound.setFont(new Font("Tahoma", Font.BOLD, 14));
                lblRound.setForeground(new Color(50, 205, 50));
                lblRound.setBackground(new Color(9, 47, 5));
                lblRound.setOpaque(true);
                lblRound.setHorizontalAlignment(SwingConstants.CENTER);
                panels[5].add(lblRound);
                
                lblStake = new JLabel("Stawka");
                lblStake.setFont(new Font("Tahoma", Font.BOLD, 14));
                lblStake.setForeground(new Color(50, 205, 50));
                lblStake.setBackground(new Color(9, 47, 5));
                lblStake.setOpaque(true);
                lblStake.setHorizontalAlignment(SwingConstants.CENTER);
                panels[5].add(lblStake);
                
                lblMaxBet = new JLabel("Max Bet");
                lblMaxBet.setFont(new Font("Tahoma", Font.BOLD, 14));
                lblMaxBet.setForeground(new Color(50, 205, 50));
                lblMaxBet.setBackground(new Color(9, 47, 5));
                lblMaxBet.setOpaque(true);
                lblMaxBet.setHorizontalAlignment(SwingConstants.CENTER);
                panels[5].add(lblMaxBet);
                
                pnlPlayers.add(panels[5]);
                
                panels[4]= new JPanel(new GridLayout(4,1));
                panels[4].setBackground(new Color(5,75,8));
        panels[4].setForeground(new Color(253, 253, 253));
                lblPlayers[4] = new JLabel("");
                lblPlayers[4].setHorizontalAlignment(SwingConstants.CENTER);
                lblPlayersCoins[4] = new JLabel();
                lblPlayersBets[4] = new JLabel();
                lblPlayersCoins[4].setHorizontalAlignment(SwingConstants.RIGHT);
                lblPlayersBets[4].setHorizontalAlignment(SwingConstants.RIGHT);
                //lblPlayersBets[4].setVerticalAlignment(SwingConstants.TOP);
                //lblPlayersBets[4].setFont(new Font("Tahoma", Font.BOLD, 10));
                lblPlayersBets[4].setForeground(Color.WHITE);
                lblPlayersExchangeCards[4] = new JLabel();
                panels[4].add(lblPlayers[4]);
                panels[4].add(lblPlayersCoins[4]);
                panels[4].add(lblPlayersBets[4]);
                panels[4].add(lblPlayersExchangeCards[4]);
                pnlPlayers.add(panels[4]);
                disableButtons();


}
        /** 
         * @param name of player,which will display
         * @param previous if the player join game before the owner of PlayerFrame
         */
        public void setPlayer(String name, boolean previous){
                if(previous){
                        lblPlayers[4-counterPrev].setText(name);
                        playersNames[4-counterPrev] = name;
                        panels[4-counterPrev].setBackground(new Color(0, 102, 0));
                        panels[4-counterPrev].setOpaque(true);
                        lblPlayersCoins[4-counterPrev].setText(lblMyCoins.getText());
                        //lblPlayersCoins[4-counterPrev].setBackground(new Color(0, 102, 0));
                        //lblPlayersCoins[4-counterPrev].setOpaque(true);
                        //lblPlayersCoins[4-counterPrev].setVerticalAlignment(SwingConstants.NORTH_EAST);
                        counterPrev++;
                }
                else{
                        lblPlayers[counterNext].setText(name);
                        playersNames[counterNext] = name;
                        panels[counterNext].setBackground(new Color(0, 102, 0));
                        panels[counterNext].setOpaque(true);
                        lblPlayersCoins[counterNext].setText(lblMyCoins.getText());
                        counterNext++;
                }
        }
        public void YouAreRejecting()
        {
        	JOptionPane.showMessageDialog(this, "Once from other Players don't want play with You" );
        }
        public void YouAreRejected()
        {
        	JOptionPane.showMessageDialog(this, "Each other players don't want play with You" );
        }
        public void PlayerIsRejected(String Player)
        {
        	JOptionPane.showMessageDialog(this, "Each other Players don't want play with:"+Player );
        }
        public void YouAreNotRejected()
        {
        	JOptionPane.showMessageDialog(this, "Some frome other players don't want play with You" );
        }
        public void PlayerIsNotRejected(String Player)
        {
        	JOptionPane.showMessageDialog(this, "Some frome other Players want play with:"+Player );
        }
        public void Wait()
        {
        	JOptionPane.showMessageDialog(this, "You can't now delete any player" );
        }
        public void WaitForPlayers()
        {
        	JOptionPane.showMessageDialog(this, "Pleas, Wait for other players" );
        }
        public void StartGame()
        {
        	start = true;
        	JOptionPane.showMessageDialog(this, "The Game is started");
        	startGame.setEnabled(false);
        	for(int i =0; i<5; i++)
        	{
        		this.rejectPlayer[i].setEnabled(false);
        		this.addBot[i].setEnabled(false);
        	}
        }
        public int RejectVote(String Player)
        {
        	Object[] options = {"Yes", "No"};
        	int n = JOptionPane.showConfirmDialog(
        		    this,
        		    "Other Players want throw of the game: " + Player+ ". Are You agree with them",
        		    "voting",
        		    JOptionPane.YES_NO_OPTION);
        	return n;
        }
    void displayPlayerHands(String[] playersHands, String winners) {
        /*
            String[i] := 15charOfName + 8charOfCards
            OR        := 15charOfName + "FOLDED"
        */
        try{
        int playerCards[][] = new int [playersHands.length][4];
        this.setVisible(false);
        for(int i = 0; i < playersHands.length; i++) {
            if(playersHands[i].substring(1).equals("FOLDED**"))
                for(int j = 0; j < 4; j++) {
                    playersCardsLabels[i][j].setIcon(getCardIcon(52));
                    playerCards[i][j] = -1;
                }
            else
                for(int j = 0; j < 4; j++) {
                    System.out.println();
                  
                    playerCards[i][j] = Integer.parseInt(playersHands[i].substring(1+2*j,3+2*j));
                  
                    playersCardsLabels[i][j].setIcon(getCardIcon(playerCards[i][j]));
                }
            String nick;
            int n = Integer.parseInt(playersHands[i].substring(0, 1));
            System.out.println(n);
            nick= listener.Player(n);
            System.out.println(nick);
            if(Character.toString(winners.charAt(i)).equals("W")) {
                playersPanels[i].setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), nick, TitledBorder.LEADING, TitledBorder.TOP, null, new Color(211, 26, 244)));
                playersPanels[i].setBackground(new Color(25, 95, 28));
            }
            else {
                playersPanels[i].setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), nick, TitledBorder.LEADING, TitledBorder.TOP, null, new Color(253, 253, 253)));
                playersPanels[i].setBackground(new Color(5,75,8));
            }
        }    	//this.requestFocus();

        setContentPane(resultPanel);
        
        repaint();
      
        }
                    catch(NumberFormatException e)
                    {
                    	System.out.println("warning");
                    } 
        this.setVisible(true); 
    }
        public void setCoins(int Id, String amount){
                if (Id == listener.getNumber()){
                        lblMyCoins.setText(amount);
                        myCoins = Integer.parseInt(amount);
                }
                else{
                	if (Id > listener.getNumber())
                		Id-=Id;
                        lblPlayersCoins[Id].setText(amount);
        
        }
        }
        /**
        * @param amount amount of coins
        */
        public void setBet(int Id, String amount){
                int bet = Integer.parseInt(amount);
                if (bet>maxBet){
                        maxBet=bet;
                        lblMaxBet.setText("Max Bet: " + amount);
                }
                if (Id == listener.getNumber()){
                        lblMyBet.setText(amount);
                        myBet = bet;
                }
                else{
                	if(Id > listener.getNumber())
                		Id-=Id;
                        lblPlayersBets[Id].setText(amount);
                }
        }
        /**
        * Setting BigBlind
        * @param bBlind BigBlind
        */
        public void setBigBlind(int Id, String bBlind){
                int blind = Integer.parseInt(bBlind);
                maxBet = blind;
                lblMaxBet.setText("Max Bet: " +bBlind);
                if (Id == listener.getNumber()){
                        JOptionPane.showMessageDialog(null, "You have a big blind :" + bBlind,
                                        "Information", JOptionPane.INFORMATION_MESSAGE);
                        lblMyBet.setText(bBlind);
                        myBet = blind;
                }
                else{
                	if(Id > listener.getNumber())
                		Id-=Id;
                                lblPlayers[Id].setText(playersNames[Id] + "(Big Blind)");
                                lblPlayersBets[Id].setText(bBlind);
                }
        }
        /**
        * Setting SmallBlind
        * @param sBlind SmallBlind
        */
        public void setSmallBlind(int Id ,String sBlind){
                if (Id == listener.getNumber()){
                        int blind = Integer.parseInt(sBlind);
                        JOptionPane.showMessageDialog(null, "You have a small blind: " + sBlind,
                                        "Information", JOptionPane.INFORMATION_MESSAGE);
                        lblMyBet.setText(sBlind);
                        myBet = blind;
                }
                else{
                	if(Id > listener.getNumber())
                		Id-=Id;
                      
                                lblPlayers[Id].setText(playersNames[Id] + "(Small Blind)");
                                lblPlayersBets[Id].setText(sBlind);
                        
                }
        }
        /**
        * Displaying amount of Exchanged cards
        * @param amount amount of coins
        */
        public void displayAmountOfExchangedCards(int Id, String amount){
        	if(Id > listener.getNumber())
        		Id-=Id;
                        lblPlayersExchangeCards[Id].setText("Cards Exchanged in round:" + amount);
                
        }
        public void displayMessage(String message){
                txtaChatDisplay.append(message+"\n");
        txtaChatDisplay.setCaretPosition(txtaChatDisplay.getDocument().getLength());
        //Auto scroll here

        }
        /**
        * @param buttons possible buttons
        */
        public void enableButtons(String buttons){
                System.out.println(buttons + "<");
                while(!buttons.equals("")){
                        if (buttons.startsWith("E")){
                                btnExchange.setEnabled(true);
                                System.out.println(btnExchange.isEnabled());
                                System.out.println("EnableButtons in frame");
                        }
                        else if (buttons.startsWith("H"))
                                btnCheck.setEnabled(true);
                        else if (buttons.startsWith("B"))
                                btnBet.setEnabled(true);
                        else if (buttons.startsWith("R"))
                                btnRaise.setEnabled(true);
                        else if (buttons.startsWith("C"))
                                btnCall.setEnabled(true);
                        else if (buttons.startsWith("F"))
                                btnFold.setEnabled(true);
                        else if (buttons.startsWith("A"))
                                btnAllin.setEnabled(true);
                        //else if (buttons.startsWith("N"))
                          //      btnExchange.setEnabled(true);//?
                        buttons = buttons.substring(1);
                }
        }
        public void disableButtons(){
                        btnCheck.setEnabled(false);
                        btnBet.setEnabled(false);
                        btnRaise.setEnabled(false);
                        btnCall.setEnabled(false);
                        btnFold.setEnabled(false);
                        btnAllin.setEnabled(false);
                        btnExchange.setEnabled(false);
        }

        public void displayFold(int Id){
        	if (Id == listener.getNumber() ){
                        lblMyBet.setText("You Folded");
                }
                else{
                	if(Id > listener.getNumber())
                        Id-=Id;
                        lblPlayersBets[Id].setText("Folded");
                }
        }
        public void displayAllin(int Id){
                if (Id == listener.getNumber() ) {
                        lblMyCoins.setText("All-in");
                }
                else{
                	if(Id > listener.getNumber())
                        Id-=Id;
                	
                        lblPlayersCoins[Id].setText("All-in");
                }
        }
        public void displayStake(String stake){
                lblStake.setText("Stake: " + stake);
                for (int i = 0; i < 5; i++){
                        lblPlayersExchangeCards[i].setText("");
                }
        }
        public void displayRound(String round){
                lblRound.setText("Round: " +round);
        }
        public void deletePlayer(String name){
                int i;
                for(i=0; i<5 && !playersNames[i].equals(name); i++){}
                if(i!=5){
                        panels[i].setVisible(false);
                }
        }
    public void displayCards(int selectedCards[]) {
        for(int i = 0; i < 4; i++) {
        	System.out.println(selectedCards[i]);
            cardsButtons[i].setIcon(cards[selectedCards[i]]);
            this.repaint();
        }
    }	
    public ImageIcon getCardIcon (int i) {
        return cards[i];
    }

    public void displayPlayersList(String[] PlayersList)
	{
    	boolean p=false;
		for(int i =0; i< PlayersList.length; i++)
		{
			if(PlayersList[i].equals(myName)&&!p)
			{
				p = true;
				continue;
			}
			if(p)
			{
				this.Players[i-1].setText(PlayersList[i]);
				if(!start){
				this.rejectPlayer[i-1].setEnabled(true);
				this.addBot[i-1].setEnabled(false);
				}
			}
			else 
				this.Players[i].setText(PlayersList[i]);
			if(i<5)
			{
				if(!start){
			this.rejectPlayer[i].setEnabled(true);
			this.addBot[i].setEnabled(false);}
			}
		}if(PlayersList.length < 6)
			
		{
			if(!start){
			if(p)
					this.addBot[PlayersList.length -1].setEnabled(true);
				else
					
					this.addBot[PlayersList.length].setEnabled(true);
			}
				}
		for(int i = PlayersList.length; i< 6; i++)
		{
			if(p)
			{
				this.Players[i-1].setText("Empty");
				if(!start){
				this.rejectPlayer[i-1].setEnabled(false);
				this.addBot[i-1].setEnabled(false);}
			}
			else
			{
				this.Players[i].setText("Empty");
				if(!start){
				this.rejectPlayer[i].setEnabled(false);
				this.addBot[i].setEnabled(false);}
			}
			}
		
		if(PlayersList.length<6&&p)
		{
			if(!start){
			this.addBot[PlayersList.length-1].setEnabled(true);
			if(PlayersList.length<4)
			this.addBot[PlayersList.length].setEnabled(false);
			this.rejectPlayer[PlayersList.length-1].setEnabled(false);}
			
		}
		else if(PlayersList.length<6)
		{if(!start){
			this.addBot[PlayersList.length-2].setEnabled(true);
			if(PlayersList.length<4)
			this.addBot[PlayersList.length-1].setEnabled(false);
			this.rejectPlayer[PlayersList.length-2].setEnabled(false);}
		}
		if(PlayersList.length >= 2&& !start)
			startGame.setEnabled(true);
		else
			startGame.setEnabled(false);
		this.repaint();
	}


        public void actionPerformed(ActionEvent e)
        {
                String s = e.getActionCommand();
                System.out.println(s);
                if (s.equals("Send")){
                        String message = txtChat.getText();
                        if(!message.equals("")){
                                listener.sendMessage(message);
                                txtChat.setText("");
                        }
                }
                else if (s.equals("Check")){
                        listener.sendCheck();
                        disableButtons();
                }
                else if (s.equals("Add Bot")){
                    listener.sendAddBot();
                }
                else if (s.equals("Start Game")){
                    listener.sendStartGame();
                    startGame.setEnabled(false);
                }
                else if (s.startsWith("Delete"))
                {
                	
                	try{
                		int i = -1;
                		i = Integer.parseInt(s.substring(6));
                			if(i >= listener.getNumber())
                		listener.sendDelete(i+1);
                	else
                		listener.sendDelete(i);
                    System.out.println("Delete");
					}
					catch(NumberFormatException err)
					{
						System.out.println(err.getMessage());
					}
                
                }
                else if (s.equals("")){
                    listener.sendCheck();
                    disableButtons();
                }
                else if (s.equals("Raise") || s.equals("Bet")){
                        betFrame.setVisible(true);
                }
                else if (s.equals("Confirm")){
                        try{
                                int temp = Integer.parseInt(betFrame.getBet());
                                if( (temp+myBet)>maxBet && temp<=myCoins){
                                        listener.sendBet(temp);
                                        betFrame.setVisible(false);
                                        betFrame.clear();
                                        disableButtons();                                       
                                }
                                else{
                                        JOptionPane.showMessageDialog(null, "You should beat Max Bet" ,
                                                "B��d", JOptionPane.ERROR_MESSAGE);
                                        betFrame.clear();
                                }
                        }
                        catch(NumberFormatException ex){
                                JOptionPane.showMessageDialog(null, "Incorrect data" ,
                                                "B��d", JOptionPane.ERROR_MESSAGE);
                                        betFrame.clear();
                        }
                }
                else if (s.equals("Call")){
                        listener.sendBet(maxBet-myBet);
                        disableButtons();
                }
                else if(s.equals("All-in")){
                        listener.sendAllin();
                        disableButtons();
                }
                else if(s.equals("Fold")){
                        listener.sendFold();
                        disableButtons();
                }
                else if(s.equals("Exchange")){
                        cardSet  = "";
                        for(int i = 0; i<4; i++){
                                if(cardsButtons[i].getBorder().equals(selectedCard))
                                        cardSet = cardSet + "1";
                        else
                                cardSet = cardSet + "0";
                cardsButtons[i].setBorder(BorderFactory.createEmptyBorder());
                    }
                        listener.sendExchange(cardSet);
                        disableButtons();
                }
                else if(s.equals("Close")){
                        setContentPane(contentPane);
                        listener.sendClose();
                }
                else if(s.equals("0")) {
           if(cardsButtons[0].getBorder().equals(selectedCard))
               cardsButtons[0].setBorder(BorderFactory.createEmptyBorder());
           else
               cardsButtons[0].setBorder(selectedCard);
        }
        else if(s.equals("1")) {
            if(cardsButtons[1].getBorder().equals(selectedCard))
                cardsButtons[1].setBorder(BorderFactory.createEmptyBorder());
            else
                cardsButtons[1].setBorder(selectedCard);
        }
        else if(s.equals("2")) {
            if(cardsButtons[2].getBorder().equals(selectedCard))
                cardsButtons[2].setBorder(BorderFactory.createEmptyBorder());
            else
                cardsButtons[2].setBorder(selectedCard);
        }
        else if(s.equals("3")) {
            if(cardsButtons[3].getBorder().equals(selectedCard))
                cardsButtons[3].setBorder(BorderFactory.createEmptyBorder());
            else
                cardsButtons[3].setBorder(selectedCard);
        }
        }
        public void keyPressed(KeyEvent e)
        {
                if (KeyEvent.getKeyText(e.getKeyCode()).equals("Enter")) {
                        String message = txtChat.getText();
                        if(!message.equals("")){
                                listener.sendMessage(message);
                                txtChat.setText("");
                        }
                }
                
        }
        public void keyReleased(KeyEvent arg0)
        {}
        public void keyTyped(KeyEvent arg0)
        {}
        public void stop()
        {
        	start = false;
        }


}
